#include <conio.h>
#include "../../HeadFiles/PagesH/CartDetailPage.h"
#include "../../HeadFiles/GetTime.h"
#include "../../HeadFiles/StandardHeads.h"

extern struct CartNode * cartHead, * cartTail;
extern struct CommodityNode * commodityHead, * commodityTail;
extern struct ClientNode * clientHead, * clientTail;

void addCartToRecord(struct CartNode * cart, struct ClientNode * client);

void releaseCart(struct CartNode * head)
{
    struct CartNode * temp = head->nextCart;
    while(temp != NULL)
    {
        free(head);
        head = temp;
        temp = temp->nextCart;
    }
    free(head);
}

int cartDetailPage(struct ClientNode * nowClient)
{
    struct CartNode * displayPtr = cartHead;
    Start:
    system("cls");

    puts("type 1 for the previous cart item");
    puts("type 2 for the next cart item");
    puts("type 3 to delete this one");
    puts("type 4 to settle accounts");
    puts("type 0 to go back");
    puts(SPLIT_STR);
    if(displayPtr != NULL)
        displayThisCart(displayPtr);
    else{
        puts("Empty Cart!");
        // system("pause");
        return 1;
    }

    char opt = getche();
    while(!(opt == '1' || opt == '2' || opt == '3' || opt == '4' || opt == '0'))
    {
        puts("Illegal input detected! Try again: ");
        opt = getche();
    }
    if(opt == '1')
    {
        if(displayPtr == cartHead)
        {
            puts("No previous cart item!");
            system("pause");
        }else{
            displayPtr = displayPtr->preCart;
        }
        goto Start;
    }else if(opt == '2'){
        if(displayPtr == cartTail)
        {
            puts("No next cart item!");
            system("pause");
        }else{
            displayPtr = displayPtr->nextCart;
        }
        goto Start;
    }else if(opt == '3'){
        // 删除当前购物车节点
        delCartNode(&cartHead, &cartTail, displayPtr);
        displayPtr = cartHead;
        goto Start;
    }else if(opt == '4'){
        // 结算所有购物车内容!
        double totalPrice = 0.0;
        struct CartNode * searchPtr = cartHead;
        while(searchPtr != NULL)
        {
            totalPrice += (searchPtr->soldPrice_Discount) * (searchPtr->unitCount);
            searchPtr = searchPtr->nextCart;
        }
        if(totalPrice > nowClient->balance)
        {
            puts("Insufficient funds! Please remove some of your items!");
            system("pause");
            goto Start;
        }else{
            nowClient->balance -= totalPrice;
            searchPtr = cartHead;
            while(searchPtr != NULL)
            {
                // rebuildChain放到addCartToRecord里面了
                addCartToRecord(searchPtr, nowClient);
                searchPtr = searchPtr->nextCart;
            }
            rebuildCommodityFile(commodityHead);
            rebuildClientFile(clientHead);
            // 结算完毕之后，释放cart链表
            releaseCart(cartHead);
            cartHead = cartTail = NULL;
        }
    }else{
        return 1;
    }

    return 1;
}

extern struct RecordNode * recordHead, * recordTail;
double getPurchasePrice(char commodityName[]);
extern struct CommodityNode * commodityDisplayPtr, * commodityHead;
void addCartToRecord(struct CartNode * cartPtr, struct ClientNode * clientPtr)
{
    struct RecordNode * now = (struct RecordNode *)malloc(sizeof(struct RecordNode));
    now->preRecord = now->nextRecord = NULL;
    if(recordHead == NULL)
    {
        // 增加第一个节点
        recordHead = recordTail = now;
    }else{
        recordTail->nextRecord = now;
        now->preRecord = recordTail;
        recordTail = now;
    }

    now->isSoldOut = 1;
    memcpy(now->commodityName, cartPtr->unitName, sizeof(now->commodityName));
    memcpy(now->clientName, clientPtr->clientName, sizeof(now->clientName));
    now->unitCount = cartPtr->unitCount;
    getCurrentTime(&now->year, &now->month, &now->day);
    now->historyLv = clientPtr->lv;
    now->purchasePrice = getPurchasePrice(cartPtr->unitName);
    now->soldPrice = cartPtr->soldPrice_Discount;

    //下面为将commodity中更新库存的部分
    struct CommodityNode * searchPtr = commodityHead;
    while(searchPtr != NULL)
    {
        if(strcmp(searchPtr->commodityName, now->commodityName) == 0)
            //对现有的商品进行修改
        {
            if(now->isSoldOut == true)
                searchPtr->unitCount -= now->unitCount;
            else if(now->isSoldOut == false)
                searchPtr->unitCount += now->unitCount;
        }
        searchPtr = searchPtr->nextCommodity;
    }

    rebuildCommodityFile(commodityHead);
    rebuildRecordFile(recordHead);
}

double getPurchasePrice(char commodityName[])
{
    struct CommodityNode * searchPtr = commodityHead;
    while(searchPtr != NULL)
    {
        if(strcmp(searchPtr->commodityName, commodityName) == 0)
            return searchPtr->purchasePrice;
        searchPtr = searchPtr->nextCommodity;
    }
    printf("Fatal Error! Commodity Purchase Not Found!!\n");
    system("pause");
    return 0.0;
}